﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ArmyCreator.ViewModels
{
    public class UnitViewModel : ALoadable<DataModel.Scouad>, INotifyPropertyChanged
    {
        #region Fields
        private string name;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }


        private int id;

        public int Id
        {
            get { return id; }
            set
            {
                id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private int points;

        public int Points
        {
            get { return points; }
            set
            {
                points = value;
                NotifyPropertyChanged("Points");
            }
        }

        public ObservableCollection<ElementViewModel> ScouadElements { get; set; }
        #endregion

        public UnitViewModel()
        {
            ScouadElements = new ObservableCollection<ElementViewModel>();
        }

        protected override void Load(DataModel.Scouad model, int depth)
        {
            /* rustine */
            int start = model.Name.IndexOf('(');
            string name = model.Name.Substring(0, start).Trim();
            int points = int.Parse(model.Name.Substring(start + 1, model.Name.IndexOf("pts)") - (start + 1)));

            this.Name = name;
            this.Points = points;
            this.Id = 0; // en attendant les vraies

            if (model.Elements != null)
            {
                foreach (var scouadElement in model.Elements)
                {
                    ElementViewModel element = new ElementViewModel();
                    element.LoadData(scouadElement, depth - 1);
                    this.ScouadElements.Add(element);
                }
            }

        }


        #region NOTIFICATIONS
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
